import java.util.concurrent.CountDownLatch;
import java.util.logging.Logger;

/**
 * Task for testing ObjectPool
 * @author an.troitskaya
 */
public class TaskPool implements Runnable {
    private static final Logger logger = Logger.getLogger(TaskPool.class.getName());
    private MyObjectPool pool;
    private CountDownLatch latch;
    public TaskPool(MyObjectPool pool, CountDownLatch latch) {
        this.pool = pool;
        this.latch = latch;
    }
    public void run() {
        Object obj = null;
        try {
            obj = pool.getObject();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            logger.warning("InterruptedException in thread "+ Thread.currentThread().getName());
        }
        logger.info("Thread "+Thread.currentThread().getName()+" was checkout");
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        pool.setObject(obj);
        logger.info("Thread "+Thread.currentThread().getName()+" was checkin");
        latch.countDown();
    }
}
